package com.example.star.view.center;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.star.R;
import com.example.star.entity.User;
import com.example.star.retrofit.ApiClient;
import com.example.star.api_service.ApiUser;
import com.example.star.view.setting.PersonalActivity;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RechargeActivity extends AppCompatActivity {

    private TextView usernameText;
    private EditText amountEditText;
    private Button rechargeButton;
    private String username; // 用于存储用户名


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recharge);

        usernameText = findViewById(R.id.usernameText);
        amountEditText = findViewById(R.id.amountEditText);
        rechargeButton = findViewById(R.id.rechargeButton);

        // 从 SharedPreferences 中读取用户名
        SharedPreferences sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE);
        username = sharedPreferences.getString("username", "");
        getAndDisplayUserInfo(username);

        setRechargeButton();

    }


    private void setRechargeButton(){
        rechargeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String username = usernameText.getText().toString();
                double amount = Double.parseDouble(amountEditText.getText().toString());

                ApiUser apiUser = ApiClient.getApiUser();
                Call<String> call = apiUser.recharge(username, amount);

                call.enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {
                        if (response.isSuccessful()) {
                            Toast.makeText(RechargeActivity.this, response.body(), Toast.LENGTH_SHORT).show();
                            finish();
                        } else {
                            Toast.makeText(RechargeActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {
                        Toast.makeText(RechargeActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

    private void getAndDisplayUserInfo(String username) {
        ApiUser apiUser = ApiClient.getApiUser();
        apiUser.getUserByUsername(username).enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                if (response.isSuccessful() && response.body() != null) {
                    User user = response.body();

                    usernameText.setText(user.getUsername());
                }
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
                Toast.makeText(RechargeActivity.this, "加载信息失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
}